package com.zktr.dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import com.zktr.bean.GoodTypeBean;
import com.zktr.bean.TestBean;
import com.zktr.pojo.GoodsType;
import com.zktr.utils.CRUDDAO;
import com.zktr.utils.FillData;
import com.zktr.utils.Test;


public class GoodTypeDao {
	CRUDDAO<GoodsType> crud = new CRUDDAO<>(GoodsType.class);   
	CRUDDAO<TestBean> crudbaen = new CRUDDAO<>(TestBean.class); 
	Test test=new Test();
	public static void main(String[] args) throws ClassNotFoundException, SQLException {
		GoodTypeDao gTypeDao = new GoodTypeDao();
    /*     List<TestBean> test2 = gTypeDao.test();
		System.out.println(test2);
        for (TestBean testBean : test2) {
			System.out.println(testBean.typeid);
		}*/
	List<TestBean> test = gTypeDao.tests();
	
	List<TestBean> testBeans=gTypeDao.getNewList(test);
	System.out.println(testBeans);
     for (TestBean testBean : testBeans) {
		System.out.println(testBean);
	}
/*     Set<TestBean> set = new LinkedHashSet<TestBean>();
     set.addAll(test);
     List<TestBean> list2 = new ArrayList<TestBean>();
     list2.addAll(set);
    for (TestBean testBean : list2) {
		System.out.println(testBean);
	
	}*/
  
     
	}
	
	  public  List<TestBean> getNewList(List<TestBean> oldList){
		    HashMap<String,TestBean> tempMap= new HashMap<String,TestBean>();
		    for(TestBean person : oldList){		 
		        String temp = person.getAttrgroup();
		        boolean containsKey = tempMap.containsKey(temp);
		        if(containsKey){
		        	TestBean newPerson = new TestBean();
		            newPerson.setAttrgroup(temp);
		            newPerson.setAttrname(tempMap.get(temp).getAttrname()+person.getAttrname());
		            System.out.println(newPerson);
		            tempMap.put(temp,newPerson );	
		            
		        }
		        else{
		        	  tempMap.put(temp,person);
		        	
		        }
		      
		    }
		    List<TestBean>  newLists = new ArrayList<TestBean>();
		    for (TestBean testBean : newLists) {
    			System.out.println(testBean);
    		}
      
		
		return newLists;
		}
	
    FillData<GoodTypeBean> fillData =new FillData<GoodTypeBean>() {
    	
		@Override
		public GoodTypeBean fillData(Map<String, Object> map) {
			Iterator<String> iterator=map.keySet().iterator();
			GoodTypeBean x=new GoodTypeBean();
			while(iterator.hasNext()){
				String y=iterator.next();
				Object object=map.get(y);
				System.out.println(y);
				if(y.equals("typeid")){
					x.setTypeid(object.toString());
				}else if (y.equals("typename")) {
					x.setTypename(object.toString());
				}
				else if (y.equals("enabled")) {
					x.setEnabled(object.toString());;
				}
				else if(y.equals("attrgroup")){
					x.setAttrgroup(object.toString());
				}
			}
			return x;
		}
    };
	

    FillData<TestBean> fillDatas =new FillData<TestBean>() {
      	
      	
  		@Override
  		public TestBean fillData(Map<String, Object> map) {	    	
  			Iterator<String> iterator=map.keySet().iterator();
  			TestBean x=new TestBean();;
  			while(iterator.hasNext()){
  				
  				String y=iterator.next();
  				Object object=map.get(y);
  				if(y.equals("typeid")){
  					x.setTypeid(object.toString());
  				}else if (y.equals("typename")) {
  					x.setTypename(object.toString());
  				}
  				else if (y.equals("enabled")) {
  					x.setEnabled(object.toString());;
  				}
  				else if(y.equals("attrgroup")){
  					x.setAttrgroup(object.toString());
  				}
  				if(y.equals("attrid")){
  					x.setAttrid(object.toString());
  				}else if (y.equals("attrname")) {
  					x.setAttrname(object.toString());
  				}
  				else if (y.equals("attrtype")) {
  					x.setAttrtype(object.toString());;
  				}
  				else if(y.equals("attrvalues")){
  					x.setAttrvalues(object.toString());
  				}
  				else if(y.equals("attrindex")){
  					x.setAttrindex(object.toString());
  				}else if (y.equals("sortorder")) {
  					x.setSortorder(object.toString());
  				}
  				else if (y.equals("islinked")) {
  					x.setIslinked(object.toString());;
  				}
  			
  			}
  			return x;
  	
  		}
      };
	/**
	 * 查询总记录数
	 * @return
	 */
	public int  selectGoodsCount() {
		return crud.selectCount("select count(*) from goods_type");
	}
	/**
	 *分页查询
	 * @param pc
	 * @param ps
	 * @return
	 */
	public  List<GoodsType> selectGoods(int pc,int ps) {
		Map<String,Object []> map=new LinkedHashMap<String,Object []> ();
		map.put("select * from goods_type LIMIT ?,?",new Object[]{(pc-1)*ps,ps});
		return 	crud.selectAll(map).get(0);
	}

	public int  selectGoodsCount(int catid) {
		return crud.selectCount("select count(*) from Goods where catid=?",catid);
	}
	/**查询所有商品信息
	 * @return
	 */
	public List<GoodsType> queryAllGoods(){
    	   String sql="select * from goods_type";
    	   LinkedHashMap<String, Object[]> map = new LinkedHashMap<String,Object[]>();
    	   map.put(sql, null);
    	   return crud.selectAll(map).get(0);
       }
	public List<TestBean> tests() throws ClassNotFoundException, SQLException{
 	   String sql="select * from goods_type join attribute ON goods_type.typeid = attribute.typeid";
 	   return crudbaen.excuteQuery(sql,fillDatas);
    }
	public  List<TestBean> test() throws ClassNotFoundException, SQLException{
	 	   String sql="select typeid from goods_type ";
	 	   return crudbaen.excuteQuery(sql,fillDatas);
	    }
}
